import {takeEvery, select, call, put, take} from 'redux-saga/effects'
import axios from 'axios'
import { message } from 'antd';
// 引入自定义操作
import { ChangeAdminLoginActivate, ChangeAdminBaseInfoActivate, AdminGetDataSaga } from '../actions/admin';
// 引入状态和常量
import { ADMINLOGINSAGA, ADMINLOGOUTSAGA, 
    CHANGEADMINLOGINACTIVATE, CHANGEAADMINBASEINFOACTIVATE,
    ADMINGETDATASAGA, 
    ADMINCHANGETASKAUDITSAGA} from '../constants'
import { API_URL } from '../../env';

// 管理员登录
function* adminLoginSaga(action) {
    const obj = action.data
    const res = yield call(axios.post, API_URL + "admin/login", {...obj})
    const res_data = res.data
    if (res_data.status == "200") {
        yield put(ChangeAdminLoginActivate({isLogin: true, form_num: 0}))
        message.success("登录成功")
    } else {
        message.error("登录失败")
    }
} 

// 管理员获得数据
function* adminGetDataSaga(action) {
    const obj = action.data
    
    const res = yield call(axios.post, API_URL + "admin/get_data", {...obj})
    const res_data = res.data
    if (res_data.status == "200"){
        yield put(ChangeAdminBaseInfoActivate({form_data: res_data.data}))
        message.success("获取成功")
        console.log("taskForm is 请求后端获得数据", res)
    } else {
        message.error("获取失败")
    }
    
}

// 管理员修改任务状态
function* adminChangeTaskAuditSaga(action) {
    const obj = action.data
    const res = yield call(axios.post, API_URL + "admin/change_audit", {...obj})

}

export function* adminSaga() {
    yield takeEvery(ADMINLOGINSAGA, adminLoginSaga)
    yield takeEvery(ADMINGETDATASAGA, adminGetDataSaga)
    yield takeEvery(ADMINCHANGETASKAUDITSAGA, adminChangeTaskAuditSaga)
}


